module.exports = function(handlebars) {
  var Dates, indexOf;
  handlebars.registerHelper("log", function(a, options) {
    console.log(a);
  });
  handlebars.registerHelper("dir", function(a, options) {
    console.dir(a);
  });

  handlebars.registerHelper("lt", function(a, b, options) {
    if (a < b) {
      return options.fn(this);
    } else {
      return options.inverse(this);
    }
  });

  handlebars.registerHelper("lteq", function(a, b, options) {
    if (a <= b) {
      return options.fn(this);
    } else {
      return options.inverse(this);
    }
  });

  handlebars.registerHelper("equals", function(a, b, options) {
    if (a === b) {
      return options.fn(this);
    } else {
      return options.inverse(this);
    }
  });

  handlebars.registerHelper("eq", function(a, b, options) {
    if (a === b) {
      return options.fn(this);
    } else {
      return options.inverse(this);
    }
  });

  handlebars.registerHelper("json", function(jsonObj, options) {
    return JSON.stringify(jsonObj);
  });

  handlebars.registerHelper("is", function(a, test, options) {
    if (a + "" === test + "") {
      return options.fn(this);
    } else {
      return options.inverse(this);
    }
  });

  handlebars.registerHelper("isnt", function(a, test, options) {
    if (a !== test) {
      return options.fn(this);
    } else {
      return options.inverse(this);
    }
  });

  handlebars.registerHelper("notin", function(a, arrayStr, options) {
    var array, item, k, len;
    array = arrayStr.split(",");
    for (k = 0, len = array.length; k < len; k++) {
      item = array[k];
      if (a === item) {
        return options.inverse(this);
      }
    }
    return options.fn(this);
  });

  handlebars.registerHelper("in", function(a, arrayStr, options) {
    var array, item, k, len;
    array = arrayStr.split(",") || [];
    for (k = 0, len = array.length; k < len; k++) {
      item = array[k];
      if (a === item) {
        return options.fn(this);
      }
    }
    return options.inverse(this);
  });

  handlebars.registerHelper("nullDefault", function(a, b, options) {
    if (a) {
      return a;
    } else {
      return b;
    }
  });

  handlebars.registerHelper("formatDate", function(date, model) {
    var _retDate;
    if (date === null) {
      return "";
    }
    date = new Date(date);
    _retDate = date.getFullYear() + "-" + Dates.padNumber(date.getMonth() + 1, 2) + "-" + Dates.padNumber(date.getDate(), 2);
    if (model === "default") {
      _retDate = _retDate + " " + Dates.padNumber(date.getHours(), 2, ' ') + ":" + Dates.padNumber(date.getMinutes(), 2);
    }
    return _retDate;
  });

  handlebars.registerHelper("formatFloat", function(data, model) {
    if (data === null) {
      return "";
    }
    return data * 1000 / 1000;
  });

  handlebars.registerHelper("subtractDate", function(a, b, options) {
    var cha, checkDate, checkDate2, checkTime, checkTime2, dateArr, dateArr2, day;
    day = 24 * 60 * 60 * 1000;
    try {
      dateArr = d1.split("-");
      checkDate = new Date();
      checkDate.setFullYear(dateArr[0], dateArr[1] - 1, dateArr[2]);
      checkTime = checkDate.getTime();
      dateArr2 = d2.split("-");
      checkDate2 = new Date();
      checkDate2.setFullYear(dateArr2[0], dateArr2[1] - 1, dateArr2[2]);
      checkTime2 = checkDate2.getTime();
      cha = (checkTime - checkTime2) / day;
      return Math.ceil(cha);
    } catch (_error) {
      return false;
    }
  });

  handlebars.registerHelper("subtract30Date", function(a, b, options) {
    var c, cha, checkDate, checkDate2, checkTime, checkTime2, dateArr, dateArr2, day;
    day = 24 * 60 * 60 * 1000;
    try {
      dateArr = d1.split("-");
      checkDate = new Date();
      checkDate.setFullYear(dateArr[0], dateArr[1] - 1, dateArr[2]);
      checkTime = checkDate.getTime();
      dateArr2 = d2.split("-");
      checkDate2 = new Date();
      checkDate2.setFullYear(dateArr2[0], dateArr2[1] - 1, dateArr2[2]);
      checkTime2 = checkDate2.getTime();
      cha = (checkTime - checkTime2) / day;
      c = Math.ceil(cha);
      if (c < 31) {
        return "剩余（" + c + "）天";
      }
      return "";
    } catch (_error) {
      return false;
    }
  });

  handlebars.registerHelper("has", function(a, b, options) {
    if (a.indexOf(b) > -1) {
      return options.fn(this);
    } else {
      return options.inverse(this);
    }
  });

  handlebars.registerHelper("haslowwer", function(a, b, options) {
    a = a || "";
    a = a.toLowerCase();
    if (a.indexOf(b) > -1) {
      return options.fn(this);
    } else {
      return options.inverse(this);
    }
  });

  handlebars.registerHelper("apartPhoneNum", function(a, options) {
    a = a.replace(/\s+/g, "");
    if (a.length === 11) {
      a = a.replace(/(\d{3})(\d{4})/g, "$1 $2 ");
    } else if (a.length === 8) {
      a = a.replace(/(\d{4})/g, "$1 ");
    }
    return a;
  });

  handlebars.registerHelper("add", function(a, b, options) {
    return a + b;
  });

  handlebars.registerHelper("before", function(a, b, arr, options) {
    var i, j;
    i = indexOf(arr, a);
    j = indexOf(arr, b);
    if (j > i) {
      return options.inverse(this);
    } else {
      return options.fn(this);
    }
  });

  indexOf = function(array, value) {
    var a, i, k, len;
    for (i = k = 0, len = array.length; k < len; i = ++k) {
      a = array[i];
      if (a === value) {
        return i;
      }
    }
  };

  handlebars.registerHelper("eachsplit", function(a, options) {
    var arr, k, len, obj, result, t;
    a = a + "";
    if (a === "") {
      return a;
    }
    arr = a.split(";");
    result = new Array();
    for (k = 0, len = arr.length; k < len; k++) {
      t = arr[k];
      obj = new Object();
      obj["name"] = t.replace(/(.+?):(.+)/g, "$1");
      obj["desc"] = t.replace(/(.+?):(.+)/g, "$2");
      if (obj["name"] !== "undefined") {
        result.push(obj);
      }
    }
    return handlebars.helpers["each"](result, options);
  });

  handlebars.registerHelper("splitLast", function(a, options) {
    var arr;
    if (a === null) {
      return "";
    }
    arr = a.split(",");
    return arr[arr.length - 1];
  });

  handlebars.registerHelper("gt", function(a, b, options) {
    if (parseFloat(a) > parseFloat(b)) {
      return options.fn(this);
    }
    return options.inverse(this);
  });

  handlebars.registerHelper("getValueInMap", function(map, key, options) {
    var obj;
    map = map || {};
    for (obj in map) {
      if (obj === key) {
        return map[key];
      }
    }
    return "";
  });

  handlebars.registerHelper("getValueInList", function(objArr, value, options) {
    var k, len, obj;
    objArr = objArr || [];
    for (k = 0, len = objArr.length; k < len; k++) {
      obj = objArr[k];
      if ((obj.name + "") === (value + "")) {
        return obj.value;
      }
    }
    return "";
  });

  handlebars.registerHelper("lenclass", function(a, options) {
    if ((a || "").length < 11) {
      return "col1";
    }
    return "col2";
  });

  handlebars.registerHelper("size", function(arr, options) {
    arr = arr || [];
    return arr.length;
  });

  Dates = {};

  Dates.padNumber = function(num, count, padCharacter) {
    var lenDiff, padding;
    if (typeof padCharacter === "undefined") {
      padCharacter = '0';
    }
    lenDiff = count - String(num).length;
    padding = '';
    if (lenDiff > 0) {
      while (lenDiff--) {
        padding += padCharacter;
      }
    }
    return padding + num;
  };

  handlebars.registerHelper("eachForStr", function(str, options) {
    var arr, k, len, result, val;
    str = str + "";
    if (str === "") {
      return "";
    }
    arr = str.split(",");
    result = new Array();
    for (k = 0, len = arr.length; k < len; k++) {
      val = arr[k];
      if (val) {
        result.push({
          value: val
        });
      }
    }
    return handlebars.helpers["each"](result, options);
  });

}